﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Group_Project_Book_Store
{
    class clsValidation
    {
        /// <summary>
        /// Method validates that user entered a valid email address
        /// </summary>
        /// <param name="email">text entered by user</param>
        public static void ValidateEmail(TextBox sender)
        {
            try
            {

                if (sender.Text.Length < 0)
                {
                    sender.Tag = false;
                }
                else if (!Regex.IsMatch(sender.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
                {
                    sender.Tag = false;
                }
                else
                {
                    sender.Tag = true;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }
        /// <summary>
        /// Method called to make sure user name is valid
        /// </summary>
        /// <param name="enteredText">User entered data</param>
        /// <param name="message">Mesage to display to user if not valid</param>
        /// <param name="senderName">Lable to update with message</param>
        public static void ValidateName(TextBox sender)
        {
            try
            {

                if (sender.Text.Length <= 0)
                {
                    sender.Tag = false;
                }

                sender.Tag = true;
                }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
           
        }
        /// <summary>
        /// Method used to validate user age is between 0 and 100
        /// </summary>
        /// <param name="txtBox">TextBox object</param>
        public static void ValidatePhoneNumber(TextBox sender )
        {
            int enteredNumber;
            try
            {
                enteredNumber = Convert.ToInt32(sender);
                if (enteredNumber <= 0 || enteredNumber > 9)
                {
                    sender.Tag = false;
                }
                else
                {

                    sender.Tag = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// validate postal code
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static bool ValidatePostalCode(string State)
        {
            try
            {
                return DatabaseClasses.DatabaseManagment.lstPostalCodes.Contains(State);
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
        /// <summary>
        /// Method to make sure the information entered is only a number.
        /// </summary>
        /// <param name="img"></param>
        /// <param name="txtBox"></param>
        public static void VerifyOnlyNumeric(Image img, TextBox sender)
        {
            try
            {
                if (!sender.Text.All(Char.IsDigit) && sender.Text.Length <= 0)
                {
                    sender.Focus();
                    sender.Background = Brushes.Red;
                    img.Visibility = Visibility.Visible;
                    sender.Tag = false;
                }
                else {
                    sender.Background = null;
                    sender.Tag = true;

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
    }
}
